//
//  smListDetailPlanViewController.m
//  shimaoHouse
//
//  Created by wang on 12-6-28.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smListDetailPlanViewController.h"
#import "SEFilterControl.h"
#import "MBProgressHUD.h"
#import "smMboSmTbProject.h"
#import "smMboSmMboDB.h"
#import "smSearchListController.h"


@interface smListDetailPlanViewController ()<UIActionSheetDelegate,MBProgressHUDDelegate,UIAlertViewDelegate>

@property(strong,nonatomic)UIWebView* myWebView;
@property(strong,nonatomic)smMboSmTbProject* projectDetail;
@property(strong,nonatomic)SEFilterControl* myFilter;
@property(nonatomic)int filterValue;
@property (nonatomic, strong) MBProgressHUD* mbprogress;
@property(strong,nonatomic)NSArray* myFilterArray;
@property(strong,nonatomic)UILabel* planLabel;


@end

@implementation smListDetailPlanViewController

@synthesize myWebView=_myWebView;
@synthesize myFilter=_myFilter;
@synthesize filterValue=_filterValue;
@synthesize mbprogress=_mbprogress;
@synthesize projectDetail=_projectDetail;
@synthesize myFilterArray=_myFilterArray;
@synthesize planLabel=_planLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:YES];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"project_background.png"]]];
    
    UIImageView* titleImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"project_title_plan.png"]];
    titleImageView.frame=CGRectMake(0, 0, 768, 70);
    
    //后退
    UIButton* backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(5, 5, 118, 52);
    [backButton setImage:[UIImage imageNamed:@"back_button_up.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"back_button_down.png"] forState:UIControlStateHighlighted];
    backButton.backgroundColor=[UIColor clearColor];
    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:titleImageView];
    [self.view  addSubview:backButton];  
    
    //获取地址
    NSMutableString *googleSearch = [NSMutableString stringWithFormat:@"http://ditu.google.cn/maps?output=embed&saddr=%@",self.projectDetail.projectAdress]; 
    self.myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(20, 72, 728, 600)];
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[googleSearch stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
    [self.view addSubview:self.myWebView];
    
    //进度确认条    
    self.myFilterArray=[NSArray arrayWithObjects:@"开始", @"建设", @"竣工", @"装修", @"交付", nil];    
    self.myFilter = [[SEFilterControl alloc]initWithFrame:CGRectMake(20, 670, 728, 60) Titles:self.myFilterArray];
    [self.myFilter addTarget:self action:@selector(sliderChange) forControlEvents:UIControlEventTouchUpInside];
    self.filterValue=[self.projectDetail.projectRate intValue];
    self.myFilter.SelectedIndex=self.filterValue;
    [self.view addSubview:self.myFilter];
    
    //重现加载地图
    UIImageView* innerImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img2.png"]];
    innerImageView.frame=CGRectMake(720, 590, 20, 20);
    innerImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapReload)];
    [innerImageView addGestureRecognizer:singleTap];
    [self.view addSubview:innerImageView];
    
    //进度地址
    UILabel*addLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 745, 728, 30)];
    addLabel.font=[UIFont systemFontOfSize:22];
    [addLabel setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];    
    [addLabel setText:[[NSString alloc] initWithFormat:@"项目地址:%@",self.projectDetail.projectAdress]];
    [addLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:addLabel];
    
    //进度描述
   self.planLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 785, 728, 30)];
    self.planLabel.font=[UIFont systemFontOfSize:22];
    [self.planLabel setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];    
    [self.planLabel setText:[[NSString alloc] initWithFormat:@"项目阶段:项目正在%@阶段...",  [self getFilterNamebyRate:self.projectDetail.projectRate]]];
    [self.planLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.planLabel];
    
    //修改时间
    UILabel*modifyDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 825, 728, 30)];
    modifyDateLabel.font=[UIFont systemFontOfSize:22];
    [modifyDateLabel setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];   
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    [modifyDateLabel setText:[[NSString alloc] initWithFormat:@"修改时间:",[dateFormatter stringFromDate:self.projectDetail.modifyDate]]];
    [modifyDateLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:modifyDateLabel];
    
    //操作者
    UILabel*operatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 868, 728, 30)];
    operatorLabel.font=[UIFont systemFontOfSize:22];
    [operatorLabel setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];    [operatorLabel setText:[[NSString alloc] initWithFormat:@"操作者ID:%@",self.projectDetail.lastOperator]];
    [operatorLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:operatorLabel];
      
    self.filterValue=self.myFilter.SelectedIndex;
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}


-(void)mapReload{
    [self.myWebView reload];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)sliderChange{
    
    if(self.filterValue!=self.myFilter.SelectedIndex){
    UIActionSheet *actionSheet = [[UIActionSheet alloc]  initWithTitle:@"确认更新项目进度？" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"确认" otherButtonTitles:@"取消",nil];
    
    [actionSheet showInView:self.view];  
    }
    
    
}

-(void) backClick:(UIButton*) bt{
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    if(buttonIndex == [actionSheet destructiveButtonIndex])
    {
         
        @try {
            [self showMbprogress];
            smMboSmTbProject *project = [smMboSmTbProject findByPrimaryKey:self.projectDetail.id_];
            if (project) {
                project.projectRate = [[NSString alloc ] initWithFormat:@"%d",self.myFilter.SelectedIndex];
                //project.modifyDate =[NSData data];
                [project save];
                [project submitPending];
                [smMboSmMboDB synchronize];
                self.filterValue=self.myFilter.SelectedIndex;
                [self performSelector:@selector(hideMbProgressSuccess) withObject:nil afterDelay:1];   
            }  
        }
        @catch (NSException *exception) {
            self.myFilter.SelectedIndex=self.filterValue;
            NSLog(@"%@: %@", [exception name],[exception reason]);
            [self performSelector:@selector(hideMbProgressFail) withObject:nil afterDelay:0.5];   
            return;
        }
       
    }
    else {        
        self.myFilter.SelectedIndex=self.filterValue;
    }
}




-(void) showMbprogress {
    
    self.mbprogress = [[MBProgressHUD alloc]initWithFrame:self.view.bounds];
    [self.mbprogress setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];
    self.mbprogress.labelText = @"正在加载";
    self.mbprogress.delegate = self;
    [self.view addSubview:self.mbprogress];
    [self.mbprogress show:YES];
    
}


-(void) hideMbProgressSuccess {
    [self.mbprogress hide:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"项目更新" message:@"更新成功!"
                                                 delegate:self cancelButtonTitle:nil otherButtonTitles: @"OK",nil];
    [alert show];
    //修改下面文字 
    [self.planLabel setText:[[NSString alloc] initWithFormat:@"项目阶段:项目正在%@阶段...",  [self getFilterNamebyRate:[[NSString alloc] initWithFormat:@"%d",self.filterValue]]]];
}

-(void) hideMbProgressFail {
    [self.mbprogress hide:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"项目更新" message:@"服务器忙，更新失败!"
                                                   delegate:self cancelButtonTitle:nil otherButtonTitles: @"OK",nil];
    [alert show];
}

-(smMboSmTbProject*)getProjectDetail:(smMboSmTbProject*)project{
    
    self.projectDetail=project;
    
    return self.projectDetail;
}

-(NSString*)getFilterNamebyRate:(NSString*)rate{

    return [self.myFilterArray objectAtIndex:[rate intValue]];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    smSearchListController* listController = [[smSearchListController alloc] init];
    [self.navigationController pushViewController:listController animated:YES];
}


@end
